package com.jetgege.smartbox.Me;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;

import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.R;
import com.jetgege.smartbox.Tools.Common;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class DevicePWDActivity extends BaseActivity {

    @BindView(R.id.devicepwd_toolbar)
    Toolbar devicepwdToolbar;
    @BindView(R.id.et_DevicePWDInput)
    EditText etDevicePWDInput;
    @BindView(R.id.ib_DevicePWDClear)
    ImageButton ibDevicePWDClear;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_pwd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initValue();

    }

    private void initValue() {

        devicepwdToolbar.setTitle("盒子密码");
        setSupportActionBar(devicepwdToolbar);
        devicepwdToolbar.setNavigationIcon(R.drawable.bar_back_btn);
        devicepwdToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            }
        });
        //调出键盘
        Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) etDevicePWDInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etDevicePWDInput, 0);
            }
        }, 500);

        etDevicePWDInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()>0)ibDevicePWDClear.setVisibility(View.VISIBLE);
                else ibDevicePWDClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.device_pwd, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        modifyTips();
        return true;
    }

    @OnClick(R.id.ib_DevicePWDClear)
    public void onViewClicked() {
        etDevicePWDInput.setText("");
    }

    private void modifyTips() {
        final KProgressHUD modifyHUD = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在修改密码...")
                .setCancellable(true)
                .show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                modifyHUD.setLabel("密码修改成功...");
                Common.dismissHUD(modifyHUD);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                },1200);
            }
        },1000);
    }
}
